﻿
using System;
using System.Windows;
using System.Windows.Media.Animation;
namespace WCYCReader.Utility
{
    public class AnimationHelper
    {

        public static Storyboard CreateEaseInAnimationStoryBoard(DependencyObject target, DependencyProperty targetProperty, double from, double to, TimeSpan duration)
        {
            Storyboard storyboard = new Storyboard();
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                Duration = duration,
                From = new double?(from),
                To = new double?(to),
                EasingFunction = new CubicEase()
                {
                    EasingMode = EasingMode.EaseIn
                }
            };
            DoubleAnimation doubleAnimation1 = doubleAnimation;
            storyboard.Children.Add(doubleAnimation1);
            Storyboard.SetTargetProperty(doubleAnimation1, new PropertyPath(targetProperty));
            Storyboard.SetTarget(doubleAnimation1, target);
            return storyboard;
        }

        public static Storyboard CreateEaseOutAnimationStoryBoard(DependencyObject target, DependencyProperty targetProperty, double from, double to, TimeSpan duration)
        {
            Storyboard storyboard = new Storyboard();
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                Duration = duration,
                From = new double?(from),
                To = new double?(to),
                EasingFunction = new CubicEase()
                {
                    EasingMode = EasingMode.EaseOut
                }
            };
            DoubleAnimation doubleAnimation1 = doubleAnimation;
            storyboard.Children.Add(doubleAnimation1);
            Storyboard.SetTargetProperty(doubleAnimation1, new PropertyPath(targetProperty));
            Storyboard.SetTarget(doubleAnimation1, target);
            return storyboard;
        }

        public static void FadeInOut(DependencyObject target, Storyboard sb, TimeSpan duration, bool isFadeout)
        {
            Duration duration1 = new Duration(duration);
            DoubleAnimation doubleAnimation = new DoubleAnimation()
            {
                Duration = duration1
            };
            if (!isFadeout)
            {
                doubleAnimation.From = new double?(0);
                doubleAnimation.To = new double?(1);
            }
            else
            {
                doubleAnimation.From = new double?(1);
                doubleAnimation.To = new double?(0);
            }
            sb.Duration = duration1;
            sb.Children.Add(doubleAnimation);
            Storyboard.SetTarget(doubleAnimation, target);
            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity", new object[0]));
            sb.Begin();
        }
    }
}

